package com.quanyan.runningriding.service.impl;

import com.quanyan.api.APIResponse;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.runningriding.entity.db.TbRunMusicCollection;
import com.quanyan.runningriding.entity.vo.run.req.MusicCollectionVo;
import com.quanyan.runningriding.entity.vo.run.resp.MusicCollectionInfoVo;
import com.quanyan.runningriding.mapper.RunMusicMapper;
import com.quanyan.runningriding.mapper.TbRunMusicCollectionMapper;
import com.quanyan.runningriding.service.RunMusicCollectionService;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by liubin on 2016/12/6.
 */
@Service
public class RunMusicCollectionServiceImpl implements RunMusicCollectionService{
    @Autowired
    private TbRunMusicCollectionMapper tbRunMusicCollectionMapper;

    @Autowired
    private RunMusicMapper runMusicMapper;

    @Override
    public APIResponse<?> insertMusicCollection(MusicCollectionVo musicCollectionVo) {
        TbRunMusicCollection tbRunMusicCollection = new TbRunMusicCollection();
        tbRunMusicCollection.setMusicCollectionName(musicCollectionVo.getMusicCollectionName());
        tbRunMusicCollection.setDescription(musicCollectionVo.getDescription());
        tbRunMusicCollection.setImgUrl(musicCollectionVo.getImgUrl());
        tbRunMusicCollection.setCreatorId(musicCollectionVo.getCreatorId());
        tbRunMusicCollection.setCreatorName(musicCollectionVo.getCreatorName());
        tbRunMusicCollection.setIsCareSelected(musicCollectionVo.getIsCareSelected());
        tbRunMusicCollection.setInitialListenPersonNum(musicCollectionVo.getInitialListenPersonNum());
        int reuslt = tbRunMusicCollectionMapper.insertSelective(tbRunMusicCollection);
        if (reuslt > 0){
            return APIResponse.returnSuccess("新增成功");
        }else {
            return APIResponse.returnFail("新增失败");
        }
    }


    @Override
    public APIResponse<?> updateMusicCollection(MusicCollectionVo musicCollectionVo) {
        TbRunMusicCollection tbRunMusicCollection = new TbRunMusicCollection();
        tbRunMusicCollection.setId(musicCollectionVo.getId());
        tbRunMusicCollection.setMusicCollectionName(musicCollectionVo.getMusicCollectionName());
        tbRunMusicCollection.setDescription(musicCollectionVo.getDescription());
        tbRunMusicCollection.setImgUrl(musicCollectionVo.getImgUrl());
        tbRunMusicCollection.setCreatorId(musicCollectionVo.getCreatorId());
        tbRunMusicCollection.setCreatorName(musicCollectionVo.getCreatorName());
        tbRunMusicCollection.setIsCareSelected(musicCollectionVo.getIsCareSelected());
        tbRunMusicCollection.setInitialListenPersonNum(musicCollectionVo.getInitialListenPersonNum());
        int reuslt = tbRunMusicCollectionMapper.updateByPrimaryKeySelective(tbRunMusicCollection);
        if (reuslt > 0)
            return APIResponse.returnSuccess("跟新成功");
        return APIResponse.returnFail("跟新异常");
    }

    @Override
    public APIResponse<?> deleteMusicCollection(Integer musicCollectionId) {
        if (musicCollectionId == 0 || musicCollectionId == null) {
            APIResponse.returnFail("参数异常");
        }
        Integer i = tbRunMusicCollectionMapper.deleteByPrimaryKey(musicCollectionId);
        if (i > 0) {
            return APIResponse.returnSuccess("删除成功");
        }
        return APIResponse.returnFail("删除异常");
    }

    @Override
    public PageObj<List<MusicCollectionInfoVo>> musicCollectionList(Map<String, Object> paramMap) {
        Integer pageNum = MapUtils.getInteger(paramMap, "pageNum");
        Integer pageSize = MapUtils.getInteger(paramMap, "pageSize");
        if (pageNum == null || pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize=20;
        }
        paramMap.put("pageStart", (pageNum - 1) * pageSize);
        List<MusicCollectionInfoVo> musicCollectionInfoVos=runMusicMapper.musicCollectionList(paramMap);
        Integer total=runMusicMapper.musicCollectionListCount(paramMap)==null?0:runMusicMapper.musicCollectionListCount(paramMap);
        PageObj<List<MusicCollectionInfoVo>> result=PageObj.create(total, pageNum, pageSize, musicCollectionInfoVos);
        return result;
    }
}
